<template>
  <vab-dialog v-model="isShowDialog" :close-on-click-modal="true" show-fullscreen width="60%">
    <template #default>
      <CommonForm
        ref="commonFormRef"
        v-model="form"
        v-loading="formLoading"
        :layout-config="layoutConfig"
        :option="option"
        @on-cancel="onHandleCancel"
        @on-success="onHandleSuccess"
      />
    </template>
  </vab-dialog>
</template>

<script>
import CommonForm from "@/plugins/form/CommonForm.vue";
import VabDialog from "@/plugins/VabDialog/index.vue";
import { TableColumnOrFormTypeEnum } from "@/plugins/commonType.enum";
export default defineComponent({
  name: "GroupMenuEdit",
  components: {
    CommonForm,
    VabDialog
  },
  emits: { "on-success": null },
  setup(props, { expose, emit }) {
    const state = reactive({
      updateUrl: "system/groupMenu/update",
      isShowDialog: false,
      formLoading: false,
      layoutConfig: {
        //form组件布局
        column: 2
      }
    });

    const form = ref({});
    const option = {
      column: [
        {
          label: "菜单名称",
          prop: "menualias",
          type: TableColumnOrFormTypeEnum.INPUT
        },
        {
          label: "显示顺序",
          prop: "showorder",
          type: TableColumnOrFormTypeEnum.INPUTNUMBER,
          precision: 0,
          min: 0,
          max: 99999
        }
      ],
      formConfig: {
        title: "",
        isInModal: true
      },
      rules: {
        menualias: [
          {
            required: true,
            message: "请输入菜单名称",
            trigger: "blur"
          },
          {
            min: 2,
            max: 20,
            message: "菜单名称长度为2到20",
            trigger: "blur"
          }
        ],
        showorder: [
          {
            required: true,
            message: "请输入显示顺序",
            trigger: "blur"
          }
        ]
      },
      submitUrl: state.updateUrl
    };

    const showDialog = row => {
      state.formLoading = true;
      form.value.id = row.id;
      form.value.menualias = row.menualias;
      form.value.showorder = row.showorder;
      option.submitUrl = state.updateUrl;
      state.formLoading = false;
      state.isShowDialog = true;
    };

    const onHandleSuccess = () => {
      emit("on-success");
      state.isShowDialog = false;
    };

    const onHandleCancel = () => {
      state.isShowDialog = false;
    };

    expose({ showDialog });
    return {
      ...toRefs(state),
      form,
      option,
      onHandleSuccess,
      onHandleCancel
    };
  }
});
</script>
